package com.rightsidetech.http.controller.admin.home;

import com.rightsidetech.commom.base.MonthQueryRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.commom.dto.response.AdminHomeTopResp;
import com.rightsidetech.system.entity.SysAgentInfo;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysUserService;
import com.rightsidetech.trade.dtos.response.HomeAgentSaleResp;
import com.rightsidetech.trade.dtos.response.HomeMonthOrderResp;
import com.rightsidetech.trade.dtos.response.HomeMonthSaleResp;
import com.rightsidetech.trade.dtos.response.HomeSpscSaleResp;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.rightsidetech.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

/**
 * @Package: com.rightsidetech.http.controller.admin.home
 * @ClassName: AdminHomeController
 * @Author: yinj
 * @CreateTime: 2021/12/22 11:25
 * @Description: 总平台首页
 */
@Api(tags = "总平台首页")
@RestController
@RequestMapping("/admin/home")
@ShenyuSpringMvcClient(path = "/admin/home/**")
@Slf4j
public class AdminHomeController extends BaseController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private SysAgentInfoService sysAgentInfoService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("顶部数据")
    @GetMapping("/top")
    public ResponseData<AdminHomeTopResp> top(){

        SysUserLoginInfo loginInfo = sysUserService.selectUserById(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        Long areaId = loginInfo.getSysCompanyAreaId();
        //订单统计
        AdminHomeTopResp adminHomeTopResp = tdCommodityUserOrderService.orderStatis(areaId);

        //用户统计
        AdminHomeTopResp userResp = userInfoService.userStatis();

        //代理商 门店
        AdminHomeTopResp sysUserResp = sysAgentInfoService.sysUserStatis(areaId);

        adminHomeTopResp.setYesterdayUser(userResp.getYesterdayUser());
        adminHomeTopResp.setTotalUser(userResp.getTotalUser());
        adminHomeTopResp.setTotalAgent(sysUserResp.getTotalAgent());
        adminHomeTopResp.setTotalStore(sysUserResp.getTotalStore());
        adminHomeTopResp.setSecondhandMarket(0L);
        adminHomeTopResp.setRoadRescue(0L);
        return ResponseData.success(adminHomeTopResp);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("当日电池销售按型号统计")
    @GetMapping("/today_specSale")
    public ResponseData<List<HomeSpscSaleResp>> todaySpecSale(){

        SysUserLoginInfo loginInfo = sysUserService.selectUserById(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
    }
        Long areaId = loginInfo.getSysCompanyAreaId();

        List<HomeSpscSaleResp> list = tdCommodityUserOrderService.todaySpecSale(areaId, null);
        return ResponseData.success(list);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("当日代理商销量排名top5")
    @GetMapping("/today_agent_ranking")
    public ResponseData<List<HomeAgentSaleResp>> todayAgentRanking(){

        SysUserLoginInfo loginInfo = sysUserService.selectUserById(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        Long areaId = loginInfo.getSysCompanyAreaId();

        List<HomeAgentSaleResp> list = tdCommodityUserOrderService.todayAgentRanking(areaId);
        if (!CollectionUtils.isEmpty(list)){
            //查询代理商名称
            for (HomeAgentSaleResp resp : list){
                SysAgentInfo sysAgentInfo = sysAgentInfoService.selectSysAgentInfo(resp.getAgentId());
                if (!ObjectUtils.isEmpty(sysAgentInfo)){
                    resp.setAgentName(sysAgentInfo.getName());
                }
            }
        }

        return ResponseData.success(list);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("电池销售按月统计")
    @GetMapping("/month_sale")
    public ResponseData<List<HomeMonthSaleResp>> monthSale(MonthQueryRequest request){
        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserService.selectUserById(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        Long areaId = loginInfo.getSysCompanyAreaId();

        return tdCommodityUserOrderService.monthSale(request,areaId,null);

    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("电池订单按月统计")
    @GetMapping("/month_order")
    public ResponseData<List<HomeMonthOrderResp>> monthOrder(MonthQueryRequest request){
        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserService.selectUserById(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        Long areaId = loginInfo.getSysCompanyAreaId();

        return tdCommodityUserOrderService.monthOrder(request,areaId);

    }

}
